﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.VehicleReport.Model;
using Yipusoft.VehicleReport.BLL;
using System.Web.UI.HtmlControls;
using Yipusoft.Account.BLL;
using Yipusoft.Account.Model;
using System.IO;
using System.Text;
using Yipusoft.Vehicle.BLL;
using Yipusoft.Vehicle.Model;

public partial class Police_SecurityEvaluate_VehicleReportStat : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {


        init();



    }

    void init()
    {
        BindDate();
        bindDays();
        ddlDay.SelectedValue = (DateTime.Now.Day - 1).ToString();
        BindData();
    }

    void BindDate()
    {
        for (int i = 2009; i < 2050; i++)
        {
            ddlYear.Items.Add(new ListItem(i + "年", i.ToString()));
        }
        ddlYear.SelectedValue = DateTime.Now.Year.ToString();
        for (int i = 1; i <= 12; i++)
        {
            ddlMonth.Items.Add(new ListItem(i + "月", i.ToString()));
        }
        ddlMonth.SelectedValue = DateTime.Now.Month.ToString();

    }

    void BindData()
    {
        DateTime date = new DateTime(Convert.ToInt32(ddlYear.SelectedValue), Convert.ToInt32(ddlMonth.SelectedValue), Convert.ToInt32(ddlDay.SelectedValue));
        string DepartmentId = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;
        lbldate.Text = date.ToString("yyyy年MM月dd日") + Department.GetDepartment(DepartmentId.Substring(0, 4) + "000").AliasName + "支队车辆检查情况报表";
        if (DepartmentId.Substring(0, 4) != "1300")
        {
            table.Rows.Clear();
            //头
            HtmlTableRow tr = new HtmlTableRow();
            HtmlTableCell td = new HtmlTableCell();
            td.InnerText = "单位";
            td.Width = "15%";
            td.Attributes.Add("style", "border-color:#888888;border-collapse:  collapse; padding: 10px; white-space:nowrap;  border-width: 1px; border-style:solid;text-align: left;");
            tr.Cells.Add(td);

            td = new HtmlTableCell();
            td.InnerText = "车辆";
            td.Width = "18%";
            td.Attributes.Add("style", "border-color:#888888;border-collapse:  collapse; padding: 10px; white-space:nowrap;  border-width: 1px; border-style:solid;text-align: left;");
            tr.Cells.Add(td);

            td = new HtmlTableCell();
            td.Attributes.Add("style", "border-color:#888888;border-collapse:  collapse; padding: 10px; white-space:nowrap;  border-width: 1px; border-style:solid;text-align: left;");
            td.InnerText = "车辆情况";
            tr.Cells.Add(td);

            td = new HtmlTableCell();
            td.Attributes.Add("style", "border-color:#888888;border-collapse:  collapse; padding: 10px; white-space:nowrap;  border-width: 1px; border-style:solid;text-align: left;");
            td.InnerText = "维修完成确认";
            tr.Cells.Add(td);
            table.Rows.Add(tr);


            //车辆损坏单位的单位IDList
            List<string> DepartmentIdList = MendRecord.GetMendDepartmentId(DepartmentId);
            foreach (string SquadronId in DepartmentIdList)
            {
                HtmlTableCell DepartmentCell = new HtmlTableCell();
                DepartmentCell.InnerText = Department.GetDepartment(SquadronId).ShortName;
                DepartmentCell.Attributes.Add("style", "border-color:#888888;border-collapse:  collapse; padding: 10px; white-space:nowrap;  border-width: 1px; border-style:solid;text-align: left;");

                string vecihelWhere = " DepartmentId='" + SquadronId + "' and ZhongdOk='' ";
                List<MendRecordInfo> vehicleList = MendRecord.GetList(vecihelWhere);
                int vehicleCount = vehicleList.Count;
                for (int i = 0; i < vehicleCount; i++)
                {
                    tr = new HtmlTableRow();
                    if (i == 0)
                    {
                        tr.Cells.Add(DepartmentCell);
                    }
                    HtmlTableCell vehicleCell = new HtmlTableCell();

                    VehicleInfo vinfo = Vehicle.GetDetail(vehicleList[i].VehicleID);
if(vinfo!=null)
{
                    vehicleCell.InnerHtml = vinfo.VehicleName + "<br>【" + vinfo.NumberPlate + "】";
}else
{
vehicleCell.InnerHtml ="此车辆已被删除！";
}
                    vehicleCell.Attributes.Add("style", "border-color:#888888;border-collapse:  collapse; padding: 10px; white-space:nowrap;  border-width: 1px; border-style:solid;text-align: left;");

                    tr.Cells.Add(vehicleCell);

                    HtmlTableCell RemarkCell = new HtmlTableCell();
                    RemarkCell.InnerHtml = vehicleList[i].Remark;
                    RemarkCell.Attributes.Add("style", "border-color:#888888;border-collapse:  collapse; padding: 10px; white-space:normal;  border-width: 1px; border-style:solid;text-align: left;");

                    tr.Cells.Add(RemarkCell);
                    HtmlTableCell OKCell = new HtmlTableCell();
                    OKCell.InnerHtml = vehicleList[i].DZDOk;

                    Button btnOk = new Button();
                    btnOk.Text = "确认";
                    btnOk.CommandName = "OK";
                    btnOk.CommandArgument = vehicleList[i].ID.ToString();

                    btnOk.Command += new CommandEventHandler(btnOk_Command);

                    OKCell.Controls.Add(btnOk);
                    OKCell.Attributes.Add("style", "border-color:#888888;border-collapse:  collapse; padding: 10px; white-space:normal;  border-width: 1px; border-style:solid;text-align: left;");

                    tr.Cells.Add(OKCell);

                    table.Rows.Add(tr);


                }
                DepartmentCell.RowSpan = vehicleCount;

            }

            //签字

            string where = " DepartmentId='" + DepartmentId + "' and ReportDate='" + date + "'  ";

            VehicleDayReportInfo report = VehicleDayReport.GetList(where).FirstOrDefault();
            if (report == null)
            {
                txtSign0.Visible = false;
                txtSign1.Visible = false;
                txtSign2.Visible = false;

                lblSign0.Visible = true;
                lblSign1.Visible = true;
                lblSign2.Visible = true;

                txtSign0.Value = "";
                txtSign1.Value = "";
                txtSign2.Value = "";

                lblSign0.Text = "";
                lblSign1.Text = "";
                lblSign2.Text = "";


                btnSave.Visible = false;

            }
            else
            {
                txtSign0.Value = report.Sign0;
                txtSign1.Value = report.Sign1;
                txtSign2.Value = report.Sign2;

                lblSign0.Text = report.Sign0;
                lblSign1.Text = report.Sign1;
                lblSign2.Text = report.Sign2;


                if (report.ReportDate == DateTime.Now.Date.AddDays(-1))
                {
                    txtSign0.Visible = true;
                    txtSign1.Visible = true;
                    txtSign2.Visible = true;

                    lblSign0.Visible = false;
                    lblSign1.Visible = false;
                    lblSign2.Visible = false;

                    btnSave.Visible = true;
                }
                else
                {
                    txtSign0.Visible = false;
                    txtSign1.Visible = false;
                    txtSign2.Visible = false;

                    lblSign0.Visible = true;
                    lblSign1.Visible = true;
                    lblSign2.Visible = true;

                    btnSave.Visible = false;
                }
            }

            //未填写中队
            List<string> NoReportDepartments = VehicleDayItem.GetNoReportDepartment(date, DepartmentId);
            string Departmentstr = "";
            for (int i = 0; i < NoReportDepartments.Count; i++)
            {
                Departmentstr += NoReportDepartments[i] + "&nbsp;&nbsp;&nbsp;&nbsp;";
            }
            lblDeparment.Text = Departmentstr;
        }
        else
        {

            return;
        }
    }

    void btnOk_Command(object sender, CommandEventArgs e)
    {
        Guid id = new Guid(e.CommandArgument.ToString());
        MendRecordInfo Reco = MendRecord.GetDetail(id);
        Yipusoft.Account.Model.UserInfo user = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name);
        string departmentName = Yipusoft.Account.BLL.Department.GetDepartment(user.DepartmentID).ShortName;
        Reco.DZDOk += Reco.DZDOk == "" ? "" : "<br/><br/>";
        Reco.DZDOk += departmentName + "<br/>" + user.UserName + "<br/>" + DateTime.Now.ToShortDateString();
        MendRecord.Update(Reco);

    }




    void BindPrintData()
    {
        DateTime date = new DateTime(Convert.ToInt32(ddlYear.SelectedValue), Convert.ToInt32(ddlMonth.SelectedValue), Convert.ToInt32(ddlDay.SelectedValue));
        string DepartmentId = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;
        lbldate.Text = date.ToString("yyyy年MM月dd日") + Department.GetDepartment(DepartmentId.Substring(0, 4) + "000").AliasName + "支队车辆检查情况报表";
        if (DepartmentId.Substring(0, 4) != "1300")
        {

            //头
            HtmlTableRow tr = new HtmlTableRow();
            HtmlTableCell td = new HtmlTableCell();
            td.InnerText = "单位";
            td.Width = "15%";
            td.Attributes.Add("style", "border-color:#888888;border-collapse:  collapse; padding: 10px; white-space:nowrap;  border-width: 1px; border-style:solid;text-align: left;");
            tr.Cells.Add(td);

            td = new HtmlTableCell();
            td.InnerText = "车辆";
            td.Width = "18%";
            td.Attributes.Add("style", "border-color:#888888;border-collapse:  collapse; padding: 10px; white-space:nowrap;  border-width: 1px; border-style:solid;text-align: left;");
            tr.Cells.Add(td);

            td = new HtmlTableCell();
            td.Attributes.Add("style", "border-color:#888888;border-collapse:  collapse; padding: 10px; white-space:nowrap;  border-width: 1px; border-style:solid;text-align: left;");
            td.InnerText = "车辆情况";
            tr.Cells.Add(td);
            table.Rows.Add(tr);


            //车辆损坏单位的单位IDList
            List<string> DepartmentIdList = VehicleDayItem.GetDamagedDepartment(date, DepartmentId);
            foreach (string SquadronId in DepartmentIdList)
            {
                HtmlTableCell DepartmentCell = new HtmlTableCell();
                DepartmentCell.InnerText = Department.GetDepartment(SquadronId).ShortName;
                DepartmentCell.Attributes.Add("style", "border-color:#888888;border-collapse:  collapse; padding: 10px; white-space:nowrap;  border-width: 1px; border-style:solid;text-align: left;");

                string vecihelWhere = " DepartmentId='" + SquadronId + "' and DayDate='" + date + "' and isdamaged=1 ";
                List<VehicleDayItemInfo> vehicleList = VehicleDayItem.GetList(vecihelWhere);
                int vehicleCount = vehicleList.Count;
                for (int i = 0; i < vehicleCount; i++)
                {
                    tr = new HtmlTableRow();
                    if (i == 0)
                    {
                        tr.Cells.Add(DepartmentCell);
                    }
                    HtmlTableCell vehicleCell = new HtmlTableCell();
                    vehicleCell.InnerHtml = vehicleList[i].VehicleName + "<br>【" + vehicleList[i].NumberPlate + "】";
                    vehicleCell.Attributes.Add("style", "border-color:#888888;border-collapse:  collapse; padding: 10px; white-space:nowrap;  border-width: 1px; border-style:solid;text-align: left;");

                    tr.Cells.Add(vehicleCell);

                    HtmlTableCell RemarkCell = new HtmlTableCell();
                    RemarkCell.InnerHtml = vehicleList[i].Remark;
                    RemarkCell.Attributes.Add("style", "border-color:#888888;border-collapse:  collapse; padding: 10px; white-space:nowrap;  border-width: 1px; border-style:solid;text-align: left;");

                    tr.Cells.Add(RemarkCell);
                    table.Rows.Add(tr);
                }
                DepartmentCell.RowSpan = vehicleCount;

            }

            //签字

            string where = " DepartmentId='" + DepartmentId + "' and ReportDate='" + date + "'  ";

            VehicleDayReportInfo report = VehicleDayReport.GetList(where).FirstOrDefault();

            if (report != null)
            {

                lblSign0.Text = report.Sign0 == "" ? "<span lang='EN-US'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>" : report.Sign0;
                lblSign1.Text = report.Sign1 == "" ? "<span lang='EN-US'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>" : report.Sign1;
                lblSign2.Text = report.Sign2 == "" ? "<span lang='EN-US'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>" : report.Sign2;
            }
            else
            {
                lblSign0.Text = "<span lang='EN-US'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>";
                lblSign1.Text = "<span lang='EN-US'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>";
                lblSign2.Text = "<span lang='EN-US'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>";
            }


            txtSign0.Visible = false;
            txtSign1.Visible = false;
            txtSign2.Visible = false;

            lblSign0.Visible = true;
            lblSign1.Visible = true;
            lblSign2.Visible = true;
        }
        else
        {

            return;
        }
    }

    protected void ddlWeek_SelectedIndexChanged(object sender, EventArgs e)
    {
        bindDays();
        BindData();
    }

    void bindDays()
    {
        int Days = DateTime.DaysInMonth(Convert.ToInt32(ddlYear.SelectedValue), Convert.ToInt32(ddlMonth.SelectedValue));
        ddlDay.Items.Clear();
        for (int i = 1; i <= Days; i++)
        {
            ddlDay.Items.Add(new ListItem(i + "日", i.ToString()));
        }
    }
    protected void ddlYear_SelectedIndexChanged(object sender, EventArgs e)
    {
        bindDays();
        BindData();
    }
    protected void ddlDay_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindData();

    }
    protected void LinkButton1_Click1(object sender, EventArgs e)
    {
        //保存
        DateTime date = new DateTime(Convert.ToInt32(ddlYear.SelectedValue), Convert.ToInt32(ddlMonth.SelectedValue), Convert.ToInt32(ddlDay.SelectedValue));
        string DepartmentId = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;

        string where = " DepartmentId='" + DepartmentId + "' and ReportDate='" + date + "'  ";

        VehicleDayReportInfo report = VehicleDayReport.GetList(where).FirstOrDefault();
        if (report == null)
        {
            report = new VehicleDayReportInfo();
            report.Id = Guid.NewGuid();
            report.PrintDate = DateTime.Now;
            report.ReportDate = date;
            report.DepartmentId = DepartmentId;
            report.Sign0 = "";
            report.Sign1 = "";
            report.Sign2 = "";
            report.Sign3 = "";
            report.UpdateDate = DateTime.Now;
            report.Remark = "";
            VehicleDayReport.Insert(report);
        }

        BindPrintData();
        ExportData("application/ms-word", DateTime.Now.ToString("yyMdHm") + ".doc");
        BindData();
    }

    private void ExportData(string FileType, string FileName)
    {
        Response.Charset = "GB2312";
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");

        Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
        Response.ContentType = FileType;
        this.EnableViewState = false;
        StringWriter tw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(tw);
        dv.RenderControl(hw);
        Response.Write(tw.ToString());
        Response.End();
    }

    public override void VerifyRenderingInServerForm(Control control)
    {
        //base.VerifyRenderingInServerForm(control);
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        DateTime date = new DateTime(Convert.ToInt32(ddlYear.SelectedValue), Convert.ToInt32(ddlMonth.SelectedValue), Convert.ToInt32(ddlDay.SelectedValue));
        string DepartmentId = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;

        string where = " DepartmentId='" + DepartmentId + "' and ReportDate='" + date + "'  ";

        VehicleDayReportInfo report = VehicleDayReport.GetList(where).FirstOrDefault();
        if (report != null)
        {


            report.Sign0 = txtSign0.Value;
            report.Sign1 = txtSign1.Value;
            report.Sign2 = txtSign2.Value;
            report.UpdateDate = DateTime.Now;
            VehicleDayReport.update(report);
        }
        BindData();
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        BindData();
    }
}
